#!/usr/bin/env python
# coding:utf-8

import os,sys,common.db
import time,datetime
from logging import *
from common.date_time import *


def clear_statinfo(totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList):
    totalCount[0] = 0
    maleCount[0] = 0
    femaleCount[0] = 0
    onlineCountList[0] = 0
    onlineCountList[1] = 0
    onlineCountList[2] = 0
    onlineCountList[3] = 0
    onlineCountList[4] = 0
    onlineCountList[5] = 0
    onlineCountList[6] = 0
    onlineCountList[7] = 0
    loginDaysList[0] = 0
    loginDaysList[1] = 0
    loginDaysList[2] = 0
    loginDaysList[3] = 0
    loginDaysList[4] = 0
    loginDaysList[5] = 0
    expCountList[0] = 0
    expCountList[1] = 0
    expCountList[2] = 0
    expCountList[3] = 0
    expCountList[4] = 0
    payCount[0] = 0
    payMoney[0] = 0
    payCountList[0] = 0
    payCountList[1] = 0
    payCountList[2] = 0
    payCount_M[0] = 0
    payCount_F[0] = 0
    payMoney_M[0] = 0
    payMoney_F[0] = 0
    gameCountList[0] = 0
    gameCountList[1] = 0
    gameCountList[2] = 0
    gameCountList[3] = 0
    gameCountList[4] = 0

def insert_statinfo(curBill,traceDate,zoneid,traceType,createDate,totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList):
    strSql = 'insert into statisticdb.DMZNewPlayerStatDay (`TraceDate`,`ZoneID`,`TraceType`,`CreateDate`,`TotalCount`,`MaleCount`,`FemaleCount`,`OnlineCount1`,`OnlineCount2`,`OnlineCount3`,`OnlineCount4`,`OnlineCount5`,`OnlineCount6`,`OnlineCount7`,`OnlineCount8`,`LoginDays1`,`LoginDays2`,`LoginDays3`,`LoginDays4`,`LoginDays5`,`LoginDays6`,`ExpCount1`,`ExpCount2`,`ExpCount3`,`ExpCount4`,`ExpCount5`,`PayCount`,`PayMoney`,`PayCount1`,`PayCount2`,`PayCount3`,`PayCount_M`,`PayCount_F`,`PayMoney_M`,`PayMoney_F`,`GameCount1`,`GameCount2`,`GameCount3`,`GameCount4`,`GameCount5`) values (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)'%(traceDate,zoneid,traceType,createDate,totalCount[0],maleCount[0],femaleCount[0],onlineCountList[0],onlineCountList[1],onlineCountList[2],onlineCountList[3],onlineCountList[4],onlineCountList[5],onlineCountList[6],onlineCountList[7],loginDaysList[0],loginDaysList[1],loginDaysList[2],loginDaysList[3],loginDaysList[4],loginDaysList[5],expCountList[0],expCountList[1],expCountList[2],expCountList[3],expCountList[4],payCount[0],payMoney[0],payCountList[0],payCountList[1],payCountList[2],payCount_M[0],payCount_F[0],payMoney_M[0],payMoney_F[0],gameCountList[0],gameCountList[1],gameCountList[2],gameCountList[3],gameCountList[4])
    #print strSql
    curBill.execute(strSql)
    

def NewPlayerStatDay(strCurDay):

    cnnBill = common.db.connect('billDB')
    curBill = cnnBill.cursor()

    # delete current day trace stat info
    strDay = strCurDay.replace('-','')
    strSql = 'delete from statisticdb.DMZNewPlayerStatDay where TraceDate=%s'%(strDay)
    nRowsCount = curBill.execute(strSql)
    cnnBill.commit()
    print 'delete new player trace stat info successful (date=%s deletedCount=%d)'%(strCurDay,nRowsCount)

    zoneList = {}

    currentDay = int(strCurDay.replace('-',''))
    threeDayAgo = int(GetDay(-3,strCurDay).replace('-',''))
    sevenDayAgo = int(GetDay(-7,strCurDay).replace('-',''))
    forteenDayAgo = int(GetDay(-14,strCurDay).replace('-',''))
    thirtyDayAgo = int(GetDay(-30,strCurDay).replace('-',''))

    # get new player trace info
    strSql = 'select * from statisticdb.DMZNewPlayerTraceDay where TraceDate=%s'%(strDay)
    curBill.execute(strSql)
    rs = curBill.fetchone()
    while rs is not None:
        if zoneList.has_key(rs[1]):
            zone = zoneList[rs[1]]
        else:
            zone = [[],[],[],[]]
        if rs[0] == rs[2]:
            zone[0].append([rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6],rs[7],rs[8],rs[9],rs[10],rs[11],rs[12],rs[13]])
        elif sevenDayAgo == rs[2]:
            zone[1].append([rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6],rs[7],rs[8],rs[9],rs[10],rs[11],rs[12],rs[13]])
        elif forteenDayAgo == rs[2]:
            zone[2].append([rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6],rs[7],rs[8],rs[9],rs[10],rs[11],rs[12],rs[13]])
        elif thirtyDayAgo == rs[2]:
            zone[3].append([rs[0],rs[1],rs[2],rs[3],rs[4],rs[5],rs[6],rs[7],rs[8],rs[9],rs[10],rs[11],rs[12],rs[13]])
        zoneList[rs[1]] = zone
        rs = curBill.fetchone()

    totalCount = [0]
    maleCount = [0]
    femaleCount = [0]
    onlineCountList = [0,0,0,0,0,0,0,0]
    loginDaysList = [0,0,0,0,0,0]
    expCountList = [0,0,0,0,0]
    payCount = [0]
    payMoney = [0]
    payCountList = [0,0,0]
    payCount_M = [0]
    payCount_F = [0]
    payMoney_M = [0]
    payMoney_F = [0]
    gameCountList = [0,0,0,0,0]

    # stat new player trace info by zone
    for zoneid in zoneList:
        zone = zoneList[zoneid]
        # stat current day new player trace info
        clear_statinfo(totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        for role in zone[0]:
            totalCount[0] += 1
            if role[4] == 1:
                maleCount[0] += 1
            else:
                femaleCount[0] += 1
            if role[8] < 300:
                onlineCountList[0] += 1
            elif role[8] < 900:
                onlineCountList[1] += 1
            elif role[8] < 3600:
                onlineCountList[2] += 1
            elif role[8] < 3600*2:
                onlineCountList[3] += 1
            else:
                onlineCountList[4] += 1
            if role[5] == 0:
                expCountList[0] += 1
            elif role[5] < 3210:
                expCountList[1] += 1
            elif role[5] < 9105:
                expCountList[2] += 1
            else:
                expCountList[3] += 1
            if role[11] != 0:
                payCount[0] += 1
                payMoney[0] += role[11]
                if role[4] == 1:
                    payCount_M[0] += 1
                    payMoney_M[0] += role[11]
                else:
                    payCount_F[0] += 1
                    payMoney_F[0] += role[11]
            if role[13] == 0:
                gameCountList[0] += 1
            elif role[13] < 5:
                gameCountList[1] += 1
            elif role[13] < 20:
                gameCountList[2] += 1
            elif role[13] < 100:
                gameCountList[3] += 1
            else:
                gameCountList[4] += 1
        insert_statinfo(curBill,currentDay,zoneid,1,currentDay,totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        # stat seven day ago new player trace info
        clear_statinfo(totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        for role in zone[1]:
            totalCount[0] += 1
            if role[4] == 1:
                maleCount[0] += 1
            else:
                femaleCount[0] += 1
            if role[8] < 900:
                onlineCountList[0] += 1
            elif role[8] < 3600:
                onlineCountList[1] += 1
            elif role[8] < 3600*4:
                onlineCountList[2] += 1
            else:
                onlineCountList[3] += 1
            if role[9] == 1:
                loginDaysList[0] += 1
            elif role[9] == 2:
                loginDaysList[1] += 1
            else:
                loginDaysList[2] += 1
            if role[10] >= threeDayAgo:
                loginDaysList[3] += 1
            if role[5] == 0:
                expCountList[0] += 1
            elif role[5] < 3210:
                expCountList[1] += 1
            elif role[5] < 9105:
                expCountList[2] += 1
            else:
                expCountList[3] += 1
            if role[11] != 0:
                payCount[0] += 1
                payMoney[0] += role[11]
                if role[12] > threeDayAgo:
                    payCountList[0] += 1
                if role[4] == 1:
                    payCount_M[0] += 1
                    payMoney_M[0] += role[11]
                else:
                    payCount_F[0] += 1
                    payMoney_F[0] += role[11]
            if role[13] == 0:
                gameCountList[0] += 1
            elif role[13] < 5:
                gameCountList[1] += 1
            elif role[13] < 20:
                gameCountList[2] += 1
            elif role[13] < 100:
                gameCountList[3] += 1
            else:
                gameCountList[4] += 1
        insert_statinfo(curBill,currentDay,zoneid,2,sevenDayAgo,totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        # stat forteen day ago new player trace info
        clear_statinfo(totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        for role in zone[2]:
            totalCount[0] += 1
            if role[4] == 1:
                maleCount[0] += 1
            else:
                femaleCount[0] += 1
            if role[8] < 900:
                onlineCountList[0] += 1
            elif role[8] < 3600:
                onlineCountList[1] += 1
            elif role[8] < 3600*4:
                onlineCountList[2] += 1
            elif role[8] < 3600*10:
                onlineCountList[3] += 1
            else:
                onlineCountList[4] += 1
            if role[9] == 1:
                loginDaysList[0] += 1
            elif (role[9] == 2) or (role[9] == 3):
                loginDaysList[1] += 1
            else:
                loginDaysList[2] += 1
            if role[10] >= sevenDayAgo:
                loginDaysList[3] += 1
            if role[5] == 0:
                expCountList[0] += 1
            elif role[5] < 3210:
                expCountList[1] += 1
            elif role[5] < 9105:
                expCountList[2] += 1
            else:
                expCountList[3] += 1
            if role[11] != 0:
                payCount[0] += 1
                payMoney[0] += role[11]
                if role[12] > sevenDayAgo:
                    payCountList[0] += 1
                if role[4] == 1:
                    payCount_M[0] += 1
                    payMoney_M[0] += role[11]
                else:
                    payCount_F[0] += 1
                    payMoney_F[0] += role[11]
            if role[13] == 0:
                gameCountList[0] += 1
            elif role[13] < 5:
                gameCountList[1] += 1
            elif role[13] < 20:
                gameCountList[2] += 1
            elif role[13] < 100:
                gameCountList[3] += 1
            else:
                gameCountList[4] += 1
        insert_statinfo(curBill,currentDay,zoneid,3,forteenDayAgo,totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        # stat thirty day ago new player trace info
        clear_statinfo(totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)
        for role in zone[3]:
            totalCount[0] += 1
            if role[4] == 1:
                maleCount[0] += 1
            else:
                femaleCount[0] += 1
            if role[8] < 900:
                onlineCountList[0] += 1
            elif role[8] < 3600:
                onlineCountList[1] += 1
            elif role[8] < 3600*4:
                onlineCountList[2] += 1
            elif role[8] < 3600*10:
                onlineCountList[3] += 1
            else:
                onlineCountList[4] += 1
            if role[9] == 1:
                loginDaysList[0] += 1
            elif (role[9] == 2) or (role[9] == 3):
                loginDaysList[1] += 1
            elif (role[9] >= 4) and (role[9] <= 6):
                loginDaysList[2] += 1
            else:
                loginDaysList[3] += 1
            if role[10] >= forteenDayAgo:
                loginDaysList[4] += 1
            if role[10] >= sevenDayAgo:
                loginDaysList[5] += 1
            if role[5] == 0:
                expCountList[0] += 1
            elif role[5] < 3210:
                expCountList[1] += 1
            elif role[5] < 9105:
                expCountList[2] += 1
            else:
                expCountList[3] += 1
            if role[11] != 0:
                payCount[0] += 1
                payMoney[0] += role[11]
                if role[12] > forteenDayAgo:
                    payCountList[0] += 1
                if role[12] > sevenDayAgo:
                    payCountList[1] += 1
                if role[4] == 1:
                    payCount_M[0] += 1
                    payMoney_M[0] += role[11]
                else:
                    payCount_F[0] += 1
                    payMoney_F[0] += role[11]
            if role[13] == 0:
                gameCountList[0] += 1
            elif role[13] < 5:
                gameCountList[1] += 1
            elif role[13] < 20:
                gameCountList[2] += 1
            elif role[13] < 100:
                gameCountList[3] += 1
            else:
                gameCountList[4] += 1
        insert_statinfo(curBill,currentDay,zoneid,4,thirtyDayAgo,totalCount,maleCount,femaleCount,onlineCountList,loginDaysList,expCountList,payCount,payMoney,payCountList,payCount_M,payCount_F,payMoney_M,payMoney_F,gameCountList)


    cnnBill.commit()

    print 'stat new player trace info success (date=%s) ...\n'%(strCurDay)

    curBill.close()
    cnnBill.close()


if __name__ != "__main__":
    
    if len(sys.argv) > 3:
        strBeginDay = sys.argv[2]
        strEndDay = sys.argv[3]
        if len(strBeginDay) != 10:
            log(ERROR,'Param is error!')
            sys.exit(0)
        if len(strEndDay) != 10:
            log(ERROR,'Param is error!')
            sys.exit(0)
        for strCurDay in intervalDay(strBeginDay,strEndDay):
            NewPlayerStatDay(strCurDay)
    elif len(sys.argv) > 2:
        strCurDay = sys.argv[2]
        if len(strCurDay) != 10:
            log(ERROR,'Param is error!')
            sys.exit(0)
        NewPlayerStatDay(strCurDay)
    else:
        strCurDay = GetDay(-1) ##"2010-01-14"
        NewPlayerStatDay(strCurDay)



